//
//  BigImageCollectionViewCell.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/18.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "BigImageCollectionViewCell.h"

#import <Photos/Photos.h>
#import "PhotoTool.h"

@interface BigImageCollectionViewCell ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIView *containerView;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIActivityIndicatorView *indicator;

@end

@implementation BigImageCollectionViewCell

- (UIScrollView *)scrollView{
    
    if (_scrollView == nil) {
        
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = self.bounds;
        _scrollView.maximumZoomScale = 3.0;
        _scrollView.minimumZoomScale = 1.0;
        _scrollView.multipleTouchEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.scrollsToTop = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleAction:)];
        [_scrollView addGestureRecognizer:singleTap];
        
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dubleAction:)];
        doubleTap.numberOfTapsRequired = 2;
        [_scrollView addGestureRecognizer:doubleTap];
        
        [singleTap requireGestureRecognizerToFail:doubleTap];
        
        
        
    }
    
    return _scrollView;
    
}

- (UIView *)containerView{
    
    if (_containerView == nil) {
        _containerView = [[UIView alloc] init];
        
    }
    
    return _containerView;
}

- (UIImageView *)imageView{
    
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
        
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    return _imageView;
}

- (UIActivityIndicatorView *)indicator{
    
    if (_indicator == nil) {
        
        _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _indicator.hidesWhenStopped = YES;
        _indicator.center = self.contentView.center;
        
    }
    
    return _indicator;
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.scrollView];
        [self.scrollView addSubview:self.containerView];
        [self.containerView addSubview:self.imageView];
        [self addSubview:self.indicator];
    }
    
    return self;
}

- (void)setAssets:(PHAsset *)assets{
    
    _assets = assets;
    
    CGFloat scale = [UIScreen mainScreen].scale;
    CGFloat width = MIN([UIScreen mainScreen].bounds.size.width, 500);
    CGSize size = CGSizeMake(width*scale, width*scale*assets.pixelHeight/assets.pixelWidth);
    [self.indicator startAnimating];
    
    __weak typeof(self) weakself = self;
    [[PhotoTool sharePhotoTool] requestImageForAsset:assets size:size resizeMode:PHImageRequestOptionsResizeModeFast completion:^(UIImage *image, NSDictionary *info) {
       
        weakself.imageView.image = image;
        [weakself setupSubviews];
        
        if (![[info objectForKey:PHImageResultIsDegradedKey] boolValue]) {
            [weakself.indicator stopAnimating];
        }
        
    }];
    
}

- (void)setupSubviews{
    
    CGRect frame;
    frame.origin = CGPointZero;
    
    UIImage *image = self.imageView.image;
    CGFloat imageScale = image.size.height / image.size.width;
    CGFloat screenScale = [UIScreen mainScreen].bounds.size.height/[UIScreen mainScreen].bounds.size.width;
    
    if (image.size.width <= self.frame.size.width && image.size.height <= self.frame.size.height) {
        
        frame.size.width = image.size.width;
        frame.size.height = image.size.height;
        
    } else {
        
        if (imageScale > screenScale) {
            frame.size.height = self.frame.size.height;
            frame.size.width = self.frame.size.height / imageScale;
            
        } else {
            
            frame.size.width = self.frame.size.width;
            frame.size.height = self.frame.size.width * imageScale;
        }
        
    }
    
    self.scrollView.zoomScale = 1;
    self.scrollView.contentSize = frame.size;
    [self.scrollView scrollRectToVisible:self.bounds animated:NO];
    
    self.containerView.frame = frame;
    self.containerView.center = self.scrollView.center;
    self.imageView.frame = self.containerView.bounds;
    
    
    
}


- (void)singleAction:(UITapGestureRecognizer *)gesture{
    
    if (self.singleTapCallBack) {
        self.singleTapCallBack();
    }
    
}


- (void)dubleAction:(UITapGestureRecognizer *)gesture{
    
    UIScrollView *scrollView = (UIScrollView *)gesture.view;
    
    CGFloat scale = 1;
    
    if (scrollView.zoomScale != 3.0) {
        scale = 3;
    } else {
        scale = 1;
    }
    
    CGRect zoomRect = [self zoomRectForScale:scale withCenter:[gesture locationInView:gesture.view]];
    
    [scrollView zoomToRect:zoomRect animated:YES];
    
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center{
    
    CGRect zoomRect;
    
    zoomRect.size.height = self.scrollView.frame.size.height/scale;
    zoomRect.size.width = self.scrollView.frame.size.width / scale;
    zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
    
    
}



- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return scrollView.subviews[0];
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    
    CGFloat offsetX = scrollView.frame.size.width > scrollView.contentSize.width ? (scrollView.frame.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    
    CGFloat offsetY = scrollView.frame.size.height > scrollView.contentSize.height ? (scrollView.frame.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
    
    self.containerView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY);
    
}



@end
